from eim.tools.dbtool import DBTool
from eim.tools.searchtool import MatchLogic,PartnerRS,SearchCondition,SearchConditionList
from eim.DataBasModals.mode import t_powers,t_operate,t_mastnetcomlog
from eim.result.page_result import PageResult
from eim.result.simple_result import SimpleResult
from flask import current_app
from sqlalchemy import func,select,update,delete,text,or_,and_,insert,join
from sqlalchemy.engine import LegacyRow,Connection,CursorResult
import time

class NetComLog:
    """
    网络设备主日志数据库读写类
    """
    def __init__(self,db):
        """
        构造函数

        Args:           
            db (_type_): 数据库连接
        """
        self.db=db
    
    def appendNewLog(self,precode:str,createtime:str,hostname:str,company:str,logversion:str,modulename:str,serverity:str,brief:str,infotype:str,serialno:str,description:str):
        """
        添加新的网络设备主日志
        Args:
            precode (str): 日志前导符
            createtime (str): 该日志的产生时间
            hostname (str): 产生该日志的主机名称
            company (str): 产生该日志的厂商或设备品牌
            logversion (str): 日志版本
            modulename (str): 产生该日志的模块名称
            serverity (str): 日志的界别
            brief (str): 日志摘要
            infotype (str): 日志类别
            serialno (str): 序列号
            description (str): 日志详细描述
        """
        
        try:
            if(hostname==""or hostname==None):
                raise Exception("主机名称不能为空")
            if(modulename==""or modulename==None):
                raise Exception("模块名称不能为空")
            if(description == "" or description==None):
                raise Exception("日志详细内容不能为空")
            vals = {
                "precode": precode,
                "createtime": createtime,
                "company": company,
                "logversion": logversion,
                "serverity": serverity,
                "brief": brief,
                "infotype": infotype,
                "serialno": serialno,
                "description": description,
                "recordtime": time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
            }
            con:Connection = self.db.engine.connect()
            qry = insert(t_mastnetcomlog).values(vals)
            res:CursorResult = con.execute(qry)
            return res.inserted_primary_key[0]
        except Exception as es:
            raise es